package com.zaimokuza.cn.service.impl;

import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.entity.like.BlogLike;
import com.zaimokuza.cn.entity.like.CommentLike;
import com.zaimokuza.cn.mapper.LikeMapper;
import com.zaimokuza.cn.service.BlogService;
import com.zaimokuza.cn.service.CommentService;
import com.zaimokuza.cn.service.LikeService;
import com.zaimokuza.cn.util.AssertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zaimokuza
 */
@Service
public class LikeServiceImpl implements LikeService {

    @Resource
    private BlogService blogService;
    @Resource
    private CommentService commentService;
    @Resource
    private LikeMapper likeMapper;

    @Override
    public void likeBlog(String blogId, String userId) {
        // 整理关注信息
        BlogLike blogLike = new BlogLike(userId, blogId);
        // 校验关注关系是否存在
        Integer isExist = likeMapper.queryBlogLikeIsExist(blogLike);
        AssertUtils.isNull(isExist, CustomizeExceptionEnum.LIKE_REPEAT);
        // 添加记录
        likeMapper.insertBlogLike(blogLike);
        // 调整相应统计信息
        blogService.increaseBlogLikes(blogId);
    }

    @Override
    public void unlikeBlog(String blogId, String userId) {
        // 整理关注信息
        BlogLike blogLike = new BlogLike(userId, blogId);
        // 校验关注关系是否存在
        Integer isExist = likeMapper.queryBlogLikeIsExist(blogLike);
        AssertUtils.notNull(isExist, CustomizeExceptionEnum.UNLIKE_REPEAT);
        // 删除记录
        likeMapper.deleteBlogLike(blogLike);
        // 调整相应统计信息
        blogService.reduceBlogLikes(blogId);
    }

    @Override
    public void likeComment(String commentId, String userId) {
        // 整理关注信息
        CommentLike commentLike = new CommentLike(userId, commentId);
        // 校验关注关系是否存在
        Integer isExist = likeMapper.queryCommentLikeIsExist(commentLike);
        AssertUtils.isNull(isExist, CustomizeExceptionEnum.LIKE_REPEAT);
        // 添加记录
        likeMapper.insertCommentLike(commentLike);
        // 调整相应统计信息
        commentService.increaseCommentLikes(commentId);
    }

    @Override
    public void unlikeComment(String commentId, String userId) {
        // 整理关注信息
        CommentLike commentLike = new CommentLike(userId, commentId);
        // 校验关注关系是否存在
        Integer isExist = likeMapper.queryCommentLikeIsExist(commentLike);
        AssertUtils.notNull(isExist, CustomizeExceptionEnum.UNLIKE_REPEAT);
        // 添加记录
        likeMapper.deleteCommentLike(commentLike);
        // 调整相应统计信息
        commentService.reduceCommentLikes(commentId);
    }

    @Override
    public void deleteBlogLikeAndCommentLikeByBlogId(String blogId) {
        // 删除博客的点赞记录
        likeMapper.deleteBatchBlogLikeByBlogId(blogId);
        // 删除博客下评论的点赞记录
        likeMapper.deleteBatchCommentLikeByBlogId(blogId);
    }

    @Override
    public void deleteCommentLikeByCommentId(String commentId) {
        likeMapper.deleteBatchCommentLikeByCommentId(commentId);
    }

}
